<template>
    <div class="edit-popup">
        <div class="flex justify-between mb-4">
            <el-button type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                添加便签
            </el-button>
        </div>
        <el-table :data="pager.lists" size="large">
            <el-table-column label="便签内容" prop="desc" min-width="300" show-overflow-tooltip />
            <el-table-column label="创建时间" prop="create_time" min-width="160" />
            <el-table-column label="操作" width="120" fixed="right">
                <template #default="{ row }">
                    <el-button type="primary" link @click="handleEdit(row)">
                        编辑
                    </el-button>
                    <el-button type="danger" link @click="handleDelete(row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="flex justify-end mt-4">
            <pagination v-model="pager" @change="getLists" />
        </div>
            
        <note-input ref="noteInputRef" @success="getLists" @close="showNoteInput = false" />
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive, shallowRef, nextTick } from 'vue'
import feedback from '@/utils/feedback'
import NoteInput from './noteInput.vue'
import { noteLists, remarkDel } from '@/api/perms/role'
import { usePaging } from '@/hooks/usePaging'

interface NoteItem {
    id: number;
    desc: string;
    create_time: string;
}

const emit = defineEmits(['success', 'close'])
const noteInputRef = shallowRef<InstanceType<typeof NoteInput>>()
const showNoteInput = ref(false)
// 便签列表数据

const { pager, getLists } = usePaging<NoteItem>({
    fetchFun: noteLists
})
// 添加便签
const handleAdd = async () => {
    showNoteInput.value = true
    await nextTick()
    noteInputRef.value?.open({}, 'add')
}

// 删除便签
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除该便签？')
    await remarkDel({ id })
    getLists()
    feedback.msgSuccess('删除成功')
}

// 编辑便签
const handleEdit = async (row: NoteItem) => {
    showNoteInput.value = true
    await nextTick()
    noteInputRef.value?.open(row, 'edit')
}

getLists()
</script>
